
// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
//        errorObject.showErro("该账户已在别的地方登录或已登出,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}

// 数字千分符转化
function comdify(n) {
  let re = /\d{1,3}(?=(\d{3})+$)/g;
  n = n.toString();
  let n1 = n.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) {
  return s1.replace(re, "$&,") + s2;
  });
  return n1;
}

// 将千分符格式的金额数字转化成普通格式的数字
function delcommafy(num){//去除千分位中的‘，’
  num = num.toString();
  num = num.replace(/,/gi, '');
  return num;
}


// 时间20161108160023 转为 2016-11-08 16:00:23
function getDateStrFormate1(dateString, type) {
    dateString = String(dateString);
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if(type == "yyyy/mm/dd hh/ii/ss"){
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1/$2/$3 $4:$5:$6');
    }else if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}
/*

format :"yyyy-mm-dd hh:ii:ss";

  return 2015-11-05 17:39:23
format :"yyyy-mm-dd";

    return 2015-11-05
format :"yyyy/mm/dd hh/ii/ss";

    return 2015/11/05 17/39/23
format :"yyyy/mm/dd";

  return 2015/11/05

 */
function getDateStrFormate2(formart, newDate) {
    var dateSty = arguments[0].split(' '),
        separatorFirst = '',
        separatorSecond = '',
        realDate = '';
    var yyyy = newDate.getFullYear(),
        mm = newDate.getMonth() + 1,
        dd = newDate.getDate(),
        hh = newDate.getHours(),
        ii = newDate.getMinutes(),
        ss = newDate.getSeconds();
    mm = mm < 10 ? '0' + mm : mm;
    dd = dd < 10 ? '0' + dd : dd;
    hh = hh < 10 ? '0' + hh : hh;
    ii = ii < 10 ? '0' + ii : ii;
    ss = ss < 10 ? '0' + ss : ss;

    if (dateSty.length > 1) {
        console.log(dateSty);
        separatorFirst = dateSty[0].charAt(4);
        separatorSecond = dateSty[1].charAt(2);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd + ' ' + hh + separatorSecond + ii + separatorSecond + ss;
    } else {
        separatorFirst = dateSty[0].charAt(4);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd;
    }

    return realDate;
}


// 计算两个时间差
function DateDiff(sDate1,  nowDate){  // sDate1,  sDate2 时间对象 sDate1早sDate2几天
    var diffMillisecond = nowDate - sDate1;
    var diffDays = diffMillisecond/  1000  /  60  /  60  /24;
    return diffDays;
}

/**
 * [isMobile 判断平台]
 * @param test: 0:iPhone    1:Android
 */
function ismobile(test){
    var u = navigator.userAgent, app = navigator.appVersion;
    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
     if(window.location.href.indexOf("?mobile")<0){
      try{
       if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
        return '0';
       }else{
        return '1';
       }
      }catch(e){}
     }
    }else if( u.indexOf('iPad') > -1){
        return '0';
    }else{
        return '1';
    }
};


module.exports = {
    haslogin:haslogin,
    comdify: comdify,
    delcommafy: delcommafy,
    getDateStrFormate1: getDateStrFormate1,
    DateDiff:DateDiff,
    ismobile:ismobile
}
